package org.grayrabbit.cms.web.servlet;

import javax.servlet.http.HttpServletRequest;

import org.grayrabbit.cms.ComponentManager;
import org.grayrabbit.cms.plugin.servlet.BaseFileServerServlet;
import org.grayrabbit.cms.plugin.servlet.DownloadStrategy;
import org.grayrabbit.cms.util.FileTypeUtil;
import org.grayrabbit.cms.util.URLCodec;


/**
 * Servlet implementation class FileServerServlet
 */
public class FileServerServlet extends BaseFileServerServlet {

	private static final long serialVersionUID = -8587353381273123931L;

	@Override
	public String getDecodedPathInfo(HttpServletRequest httpServletRequest) {
		return urlDecode(httpServletRequest.getPathInfo());
	}

	@Override
	protected DownloadStrategy instantiateDownloadStrategy(Class<? extends DownloadStrategy> downloadStrategyClass) {
		return ComponentManager.getInstance().getContainer().getComponent(downloadStrategyClass);
	}

	@Override
	public String urlDecode(String url) {
		if (url == null) {
			return null;
		}
		try {
			return URLCodec.decode(url, getCharacterEncoding());
		} catch (Exception e) {
		}
		return url;
	}

	private String getCharacterEncoding() {
		return ComponentManager.getInstance().getApplicationProperties().getEncoding();
	}

	@Override
	protected String getContentType(String location) {
		return FileTypeUtil.getContentType(location);
	}

}
